﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.BOMs;
using MySqlX.XDevAPI.Common;

namespace MyMES.Writer.API.Application.CommendHandlers.BOMs
{
    public class UpdateBOMHandler : IRequestHandler<UpdateBOMCommand, APIResult<BOM>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BOM> bom;
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="bom"></param>
        public UpdateBOMHandler(IMapper mapper,IBaseRepository<BOM> bom)
        {
            this.mapper = mapper;
            this.bom = bom;
        }

        public async Task<APIResult<BOM>> Handle(UpdateBOMCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<BOM> rusult = new APIResult<BOM>();
                rusult.Code = APIEnum.修改成功;
                rusult.Msg = "修改成功";
                var model = mapper.Map<BOM>(request);
                string aa = "update BOM set  Itemnumber=@Itemnumber,BOMunit=@BOMunit,BOMmaterial=@BOMmaterial,ExpectedUsage=@ExpectedUsage,BomType=@BomType,IsDlete=@IsDlete  where BomId = @BomId";
                await bom.Update(model, aa);
                return rusult;
            }
            catch (Exception)
            {

                throw;
            }
          
        }
    }
}
